package com.example.library_demo.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.library_demo.annotation.Auth;
import com.example.library_demo.common.Result;
import com.example.library_demo.dto.shelf.AddShelfDTO;
import com.example.library_demo.dto.shelf.UpdateShelfDTO;
import com.example.library_demo.entity.Shelf;
import com.example.library_demo.entity.User;
import com.example.library_demo.enums.GlobalResponseResultEnum;
import com.example.library_demo.service.IShelfService;
import com.example.library_demo.service.IUserService;
import com.example.library_demo.vo.shelf.ShelfVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Eric
 * @since 2024-11-27
 */
@RestController
@RequestMapping("/shelf")
public class ShelfController {

    @Autowired
    private IShelfService shelfService;
    @Autowired
    private IUserService userService;

    @Auth
    @GetMapping
    public Result<IPage<ShelfVO>> list(@RequestParam int pageNum, @RequestParam int pageSize) {
        IPage<ShelfVO> shelfVOIPage = shelfService.listShelves(new Page(pageNum, pageSize));
        return Result.success(shelfVOIPage);
    }

    @Auth
    @PostMapping
    public Result<ShelfVO> add( @RequestBody AddShelfDTO addShelfDTO) {
        Shelf shelf = new Shelf();
        BeanUtils.copyProperties(addShelfDTO, shelf);
        shelfService.save(shelf);
        ShelfVO vo = new ShelfVO();
        BeanUtils.copyProperties(shelf,vo);
        return Result.success(vo);
    }

    @Auth
    @PutMapping
    public Result updateStatus(@RequestBody UpdateShelfDTO updateShelfDTO) {
        Shelf shelf = shelfService.getById(updateShelfDTO.getShelfId());
        if (null != shelf) {
            BeanUtils.copyProperties(updateShelfDTO, shelf);
            shelfService.updateById(shelf);
            return Result.success();
        } else {
            return Result.error(GlobalResponseResultEnum.QUERY_DATA_NOT_EXIST_ERROR);
        }
    }

}

